package com.misssad.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.misssad.model.Girl;
import com.misssad.model.Result;
import com.misssad.repository.GirlRepository;
import com.misssad.service.GrilService;
import com.misssad.utils.ResultUtil;
@RestController
public class GirlController {

	private final static Logger logger = LoggerFactory.getLogger(GirlController.class);
	
	@Resource
	private GirlRepository girlRepository;
	
	@Resource
	private GrilService girlService;
	
//	private MongoTemplate mongoTemplate;
	
	/**
	 * 获取女生列表
	 * @return
	 */
	@RequestMapping(value="/list")
	public List<Map<String, Object>> girlList(){
		logger.info("girllist");
		return girlRepository.findAll();
	}
	
	/**
	 * 添加一个女生
	 * @param cupSize
	 * @param age
	 * @return
	 */
	@RequestMapping(value="/girls")
	public Result<Girl> add(@Valid Girl girl, BindingResult bindingResult){
//        mongoTemplate.save(girl);
//        mongoTemplate.insert(girl);
		if(bindingResult.hasErrors()){
			return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
        return ResultUtil.success(girlRepository.save(girl));
	}
	
	/**
	 * 查询一个女生
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/girl/{id}")
	public Document girlFindOne(@PathVariable("id") String id){
		return girlRepository.findOne(id);
	}
	
	@RequestMapping(value="/girl/update/{id}")
	public void girlUpdate(@PathVariable("id") String id,
            @RequestParam("cupSize") String cupSize,
            @RequestParam("age") Integer age){
		
		Girl girl = new Girl();
		girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        
        girlRepository.update(girl);
		
	}
	
	@RequestMapping(value = "/girl/delete/{id}")
	public void girlDelete(@PathVariable("id") String id){
		girlRepository.delete(id);
	}
	
	@GetMapping(value = "girls/getAge/{id}")
	public void getAge(@PathVariable("id") String id) throws Exception{
		girlService.getAge(id);
	}
	
}
